package com.tingyuge.thinking.spring.dependency.source;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;


/**
 * @author xiaodao
 * @version 1.0
 * @date 2021/7/4
 */
@Configuration
@PropertySource(value = "classpath:/META-INF/default.properties",encoding = "GBK")
public class ExternalConfigurationDependencySourceDemo {


    @Value("${user.id:-1}")
    private Long id;
    @Value("${usr.name}")
    private String name;
    @Value("${user.resource:classpath://default.properties}")
    private Resource resource;
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        //注册 configuration class（配置类）--->也是 spring bean
        applicationContext.register(ExternalConfigurationDependencySourceDemo.class);
        //启动应用上下文

        applicationContext.refresh();
        ExternalConfigurationDependencySourceDemo demo = applicationContext.getBean(ExternalConfigurationDependencySourceDemo.class);
        System.out.println("user.id = "+ demo.id);
        System.out.println("user.resource = "+ demo.resource);
        System.out.println("user.name = "+ demo.name);
        //显示关闭应用上下文
        applicationContext.close();
    }
}
